<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\helper;

use Yii;
use yii\bootstrap4\Alert;
use yii\helpers\Html;

/**
 * HTML5
 * @author cleverstone
 * @since ym2.0
 */
class H5 extends Html
{
    /**
     * 应用元数据标签
     * @return string
     */
    public static function appMetaTags()
    {
        $viewParams = Yii::$app->view->params;
        $themeColor = '#5bc0de';
        if (!empty($viewParams['themeColor'])) {
            $themeColor = $viewParams['themeColor'];
        }

        return static::tag('meta', '', ['name' => 'http-success-code', 'content' => App::params('http.res.success')]) . "\n"
            . static::tag('meta', '', ['name' => 'http-failure-code', 'content' => App::params('http.res.failure')]) . "\n"
            . static::tag('meta', '', ['name' => 'http-unauthorized-code', 'content' => App::params('http.res.unauthorized')]) . "\n"
            . static::tag('meta', '', ['name' => 'http-ajax-identity', 'content' => App::params('http.ajax.identity')]) . "\n"
            . static::tag('meta', '', ['name' => 'admin-route-login', 'content' => Url::to(App::params('admin.route.login'), '')]) . "\n"
            . static::tag('meta', '', ['name' => 'app-theme-color', 'content' => $themeColor]) . "\n";
    }

    /**
     * alert组件
     * @param string $message 内容
     * @param string $type 类型
     * - normal 无
     * - info 信息
     * - error 错误
     * - danger 危险
     * - success 成功
     * - warning 警告
     * @param array $options 选项
     * @return string
     * @throws \Throwable
     */
    public static function alert($message, $type = 'normal', $options = [])
    {
        $typeDefinition = [
            'normal' => 'alert-normal',
            'error'   => 'alert-danger',
            'danger'  => 'alert-danger',
            'success' => 'alert-success',
            'info'    => 'alert-info',
            'warning' => 'alert-warning'
        ];
        $alert = '';
        if ($type && !empty($typeDefinition[$type])) {
            $alert = $typeDefinition[$type];
        }

        self::addCssClass($options, ['alert' => $alert]);

        return Alert::widget([
            'body' => preg_replace('/`(.*)`/', '<code class="bg-light px-2 rounded text-dark">\\1</code>', nl2br($message)),
            'options' => $options,
        ]);
    }

    /**
     * img扩展
     * @param string $src 链接
     * @param array $options 选项
     * @return string
     */
    public static function img($src, $options = [])
    {
        if (empty($src)) return '';
        self::addCssStyle($options, ['width' => '50px', 'height' => '50px'], false);//default 50x50

        return parent::img(Url::whole($src), $options);
    }

    /**
     * 图片
     * @param string $src 图片链接
     * @param array $options 选项
     * @return string
     */
    public static function imgX($src, $options = [])
    {
        if (empty($src)) return '';
        $options['ym2-full-image'] = '';
        self::addCssClass($options, ['default' => 'img-default-ui']);//img-default-ui 70x70
        self::addCssStyle($options, ['width' => '70px', 'height' => '70px'], false);

        return self::img($src, $options);
    }
    public static function imgX30($src, $options = [])
    {
        if (empty($src)) return '';
        $options['ym2-full-image'] = '';
        self::addCssClass($options, ['default' => 'img-30']);//img-30 30x30
        self::addCssStyle($options, ['width' => '30px', 'height' => '30px'], false);

        return self::img($src, $options);
    }

    /**
     * 徽章标签
     * @param string $content 标签内容
     * @param string $color 徽章颜色
     * - primary    主题颜色
     * - pink       粉
     * - red        红
     * - orange     橙
     * - warning    黄
     * - green      绿
     * - success    深绿
     * - cyan       青
     * - blue       蓝
     * - purple     紫
     * - light      浅色
     * - gray       灰
     * - gray1      灰(二级)
     * - secondary  深灰
     * - black      黑
     * @param string $appearance 徽章外观
     * - layui-badge-dot 点
     * - layui-badge     矩形
     * @return string
     */
    public static function badge($content, $color = 'primary', $appearance = 'layui-badge')
    {
        return self::tag('span', $content, [
            'class' => ['font-weight-lighter', $appearance, "layui-bg-{$color}"],
        ]);
    }

    /**
     * 徽章标签(点)
     * @param $content
     * @param string $color
     * @see badge()
     * @return string
     */
    public static function badgeDot($content, $color = 'primary')
    {
        return self::badge($content, $color, 'layui-badge-dot');
    }

    /**
     * h5文本
     * @param string $content 文本
     * @param array $options 选项
     * @param string $tag 标签
     * @return string
     */
    public static function text($content, $options = [], $tag = 'span')
    {
        return self::tag($tag, $content, $options);
    }
}